package com.lanhao.aot.willisv2;

import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpStatus;

public class WillisV2Util {
	private static final Logger LOG = LoggerFactory.getLogger(WillisV2Util.class);

	public static final String ACCESS_KEY = "481AKDL85DAFAAEBOPOP458YUNXK09AC";
	public static final String ACCESS_SECRET = "KwThpXRJLQXSFCgSlhKARK18ZEhVoBBKQvruUpfVxlnakFgq";

	public static final String THIRD_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCHQ5YmdqJgeO/2DGS/nU02025ccZZ60DxP7o9QoE/sx9bW1+Zp5xzi/xOn14GvH4ReHY5HqTwFvhWKpeceHZavKcwFiBctYR02rYRs4ozwMI57azR/dqLMVVK2cJ1PnJAb85KYpaeIFIdRPXXGXkBa3LpKCLXfFPMFEZxkODrdJQIDAQAB";

	public static final String BASE_URL = "http://202.105.140.133:22104";
	public static final String LOGISTICS_STATUS_URL = "/ggp/ebay/ah/push/logistics/status";
	public static final String LOGISTICS_PUSH_URL = "/ggp/ebay/ah/push/logistics";
	public static final String SURVEY_PUSH_URL = "/ggp/ebay/ah/receive/goods/notity";

	public static void main(String[] args) {
//		syncLogisticsStatus();
//		pushWaybillNo();
		
		pushSurveyResult();
	}

	/**
	 * 同步物流状态
	 */
	public static Pair<Boolean, String> syncLogisticsStatus() {
		JSONObject data = new JSONObject();
		data.put("saleOrderId", "90000000000004");
		data.put("logisticsOrderNo", "00HVM0BE");
		data.put("logisticsCompanyCode", "GLS");
		data.put("logisticsStatus", "SHIPPING");
		data.put("occurrenceDate", "2024-02-20 07:23:44");
		data.put("occurrenceLocation", "CC Office");
		data.put("occurrenceLocation", "xxxxxxxxxxxxx");
		data.put("traceStatus", "SHIPPING");
		String url = BASE_URL + LOGISTICS_STATUS_URL;
		return doWillisV2Invoke(url, "物流状态同步", data);
	}

	/**
	 * 面单链接推送
	 */
	public static Pair<Boolean, String> pushWaybillNo() {
		JSONObject data = new JSONObject();
		data.put("taskNo", "8c9e2f9b4f99403888261f8e7522277f");
		data.put("businessNo", "90000000000004");
		data.put("waybillNo", "00HVM0BE");
		data.put("orderNo", "90000000000004");
		data.put("warehouseCode", "CKFRLM0001");
		data.put("waybillNoUrl", "https://res-oss.anhelogistics.com/eLabel/GLS/2024/02/20/GLS_00HVM0BE.pdf");
		data.put("logisticsCompanyCode", "GLS");
		String url = BASE_URL + LOGISTICS_PUSH_URL;
		return doWillisV2Invoke(url, "面单链接推送", data);
	}

	public static Pair<Boolean, String> pushSurveyResult() {
		JSONObject data = new JSONObject();
		data.put("saleOrderId", "90000000000004");
		data.put("logisticsOrderNo", "00HVM0BE");
		data.put("receiveGoodsDate", System.currentTimeMillis());
		data.put("collectDate", System.currentTimeMillis());
		data.put("receiveWarehouseCode", "CKFRLM0001");
		data.put("actualFreight", 54.32);
		data.put("returnQualityResult", "ACCEPTED");
		data.put("qualityResultFeedBackDate", System.currentTimeMillis());
		data.put("returnQualityDesc", "ACCEPTED");
		data.put("packageCount", "1");
		data.put("packagePicUrl", "https://res-oss.anhelogistics.com/survey/2024/02/20/6d680f5f-98c0-40db-8656-0462ee6bf927.jpg,https://res-oss.anhelogistics.com/survey/2024/02/20/be455964-14e2-4628-a908-5aa0a0b5581c.jpg");
		data.put("unpackPicUrl", "https://res-oss.anhelogistics.com/survey/2024/02/20/6d680f5f-98c0-40db-8656-0462ee6bf927.jpg,https://res-oss.anhelogistics.com/survey/2024/02/20/be455964-14e2-4628-a908-5aa0a0b5581c.jpg");
		data.put("itemGoodsPicUrl", "https://res-oss.anhelogistics.com/survey/2024/02/20/6d680f5f-98c0-40db-8656-0462ee6bf927.jpg,https://res-oss.anhelogistics.com/survey/2024/02/20/be455964-14e2-4628-a908-5aa0a0b5581c.jpg");
		data.put("itemGoodsCount", "1");
		data.put("totalPrice", 0.01);
		data.put("totalWeight", 0.45);
		data.put("totalVolume", 0);
		data.put("totalNowPrice", 0.01);
		data.put("itemGoodsName", "AnHe Goods");
		data.put("itemGoodsPricePer", 0.01);
		data.put("warehouseServerFee", 22); 
		String url = BASE_URL + SURVEY_PUSH_URL;
		return doWillisV2Invoke(url, "质检结论推送", data);
	}

	/**
	 * 通用调用方法
	 * 
	 * @param url
	 * @param interfaceName
	 * @param data
	 * @return
	 */
	public static Pair<Boolean, String> doWillisV2Invoke(String url, String interfaceName, JSONObject data) {
		try {
			String requestParam = buildRequestParam(data);
			LOG.info("调用{}接口，url={}, 请求参数requestParam={}", interfaceName, url, requestParam);
			String respBody = HttpRequest.post(url).body(requestParam).execute().body();
			LOG.info("调用{}接口，返回结果respBody={}", interfaceName, respBody);
			if (StringUtils.isNotBlank(respBody)) {
				JSONObject respObject = JSON.parseObject(respBody);
				Integer code = respObject.getInteger("code");
				String errorMessage = respObject.getString("errorMessage");
				if (code == HttpStatus.HTTP_OK) {
					return Pair.of(true, interfaceName + "成功");
				}
				return Pair.of(true, interfaceName + "失败：" + errorMessage);
			}
			return Pair.of(true, interfaceName + "失败：请求接口返回为空");
		} catch (Exception e) {
			e.printStackTrace();
			return Pair.of(true, interfaceName + "失败：" + e.getMessage());
		}
	}

	/**
	 * 构建请求参数
	 * 
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static String buildRequestParam(JSONObject data) throws Exception {
		JSONObject request = new JSONObject();
		JSONObject head = new JSONObject();
		head.put("accessKey", ACCESS_KEY);
		head.put("accessSecret", ACCESS_SECRET);
		request.put("head", head);
		String encryptData = RSAUtils.encryptByPublicKey(data.toJSONString(), THIRD_PUBLIC_KEY);
		request.put("data", encryptData);
		return request.toJSONString();
	}
}
